<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Promise实现循环变色</title>
    <style>
        .circle{
            width: 200px;
            height: 200px;
            background-color: red;
            border-radius: 50%;
        }
    </style>
</head>
<body>
<div id="circle" class="circle">
</div>
<script>
    let circle = document.getElementById('circle');
    const colors = ['red','green','yellow'];
    function changeColor(color, timeOut) {
        return new Promise((resolve, reject) => {
            setTimeout(()=>{
                resolve(color);
            },timeOut);
        });
    }

    async function changeAll() {
        let i = 0;
        while(true){
            let result = await changeColor(colors[i%3],3000)
            circle.style.backgroundColor = result;
            i++;
        }
    }
    changeAll()
</script>
</body>
</html>
